package cn.geminis.data.jpa.filter;

import cn.geminis.data.jpa.QueryPredicateBuilder;
import cn.geminis.data.jpa.entity.ClassAdviser;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.domain.Specification;

@Getter
@Setter
@Builder
public class ClassAdviserFilter implements Specification<ClassAdviser> {

    private boolean studentBookIsEmpty;
    private boolean studentBookIsNotEmpty;
    private String studentName;

    @Override
    public Predicate toPredicate(Root<ClassAdviser> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {

        var builder = new QueryPredicateBuilder<>(root, query, criteriaBuilder);
        builder.isEmpty("student.books", studentBookIsEmpty);
        builder.isNotEmpty("student.books", studentBookIsNotEmpty);
        builder.equal("clazz.students.name", studentName);

        return builder.build();
    }
}
